//
//  OMCollectionViewController.m
//  A_project2.0
//
//  Created by apple on 16/3/15.
//  Copyright © 2016年 七平. All rights reserved.
//

#import "OMCollectionViewController.h"
#import <AFNetworking.h>
#import <UIImageView+HighlightedWebCache.h>
#import "YZQMoneyCell.h"
#import "YZQCollectionModel.h"
#import "YZQMoneyModel.h"
#import "YZQHotCollectionViewCell.h"
#import "YZQButtonCollectonController.h"
#import "OMGWebViewViewController.h"
#import "YZQCityViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface OMCollectionViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView; // tableView头高
@property (nonatomic, strong) NSMutableArray *scrollArr;// 轮播图图片数组
@property (nonatomic, strong) NSMutableArray *dataArr;//  轮播图图片的数据
@property (nonatomic, copy) NSString *overview_urlStr;// 属性传值webView
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *moneyArr;// tableView的数据

@property (nonatomic, strong) NSIndexPath *index;

@end

@implementation OMCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
#pragma mark - tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // cell 重用池
    [_tableView registerClass:[YZQMoneyCell class] forCellReuseIdentifier:@"cell"];
    
    
#pragma mark - tableView上的View
    UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 460, self.view.frame.size.width, 30)];
    //    ziyouxingView.backgroundColor = [UIColor whiteColor];
    //[ziyouxingView NightWithType:UIViewColorTypeNormal];
    [_headerView addSubview:ziyouxingView];
    
#pragma mark - 超值自由行的label
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 460, 150, 30)];
    title.text = @"超值自由行";
    
    title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
    [_headerView addSubview:title];
#pragma mark - 轮播图调用方法
    [self pic];
    
    // 地图
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-ditu"] style:UIBarButtonItemStylePlain target:self action:@selector(map)];
    /**< map数据处理 */
    [self mapDataHandler];
}
- (void)mapDataHandler
{
    [QPNetWorkingTool getNetWorkingTool:[[NSString stringWithFormat:@"http://open.qyer.com/qyer/map/city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@", self.cid] stringByAppendingString:@"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.4&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1"] dic:nil success:^(id result) {
        self.mapArr = [NSMutableArray array];
        self.mapArr = result[@"data"];
    } fail:^(NSError *error) {
        
    }];
}
- (void)map
{
    self.hidesBottomBarWhenPushed = YES;
    QPMapViewController *p = [[QPMapViewController alloc]init];
    NSDictionary *dic = _mapArr[0];
    p.latStr = [dic[@"lat"] floatValue];
    p.lngStr = [dic[@"lng"] floatValue];
    p.nameStr = self.labelCnname.text;
    NSLog(@"lat:%f  lng:%f",p.latStr,p.lngStr);
    [self.navigationController pushViewController:p animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    

}
#pragma mark - 轮播图
- (void)pic
{
#pragma mark - 判断 如果是城市 执行带collectionview
    if (!([self.cid  isEqualToString:@"62"] | [self.cid isEqualToString:@"10723"] | [self.cid isEqualToString:@"10239"] |[self.cid isEqualToString:@"7789"])) {
        
        // 获取轮播图数据
        NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/country_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_id=%@",self.cid];
        NSLog(@"~~%@",self.cid);
        NSString *url = @"&lat=38.88241944084977&lon=121.5398896720327&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
        
        [QPNetWorkingTool getNetWorkingTool:[NSString stringWithFormat:@"%@%@",str,url] dic:nil success:^(id result) {
#pragma mark - tableView的header
            self.headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 490)];
            _headerView.backgroundColor = [UIColor colorWithRed: 215 / 255.0 green:215 / 255.0 blue: 215 / 255.0 alpha:1];
            self.tableView.tableHeaderView = _headerView;
            // 夜间模式
            
            NSArray *arr = result[@"data"][@"photos"];
            if (arr.count < 3) {
                UIImageView *backimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
                backimg.image = [UIImage imageNamed:@"boom.jpg"];
                [_headerView addSubview:backimg];
                
            }else{
                
#pragma mark - 轮播图数据处理
                self.scrollArr = result[@"data"][@"photos"];
                // 轮播图
                MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) imageArr:_scrollArr];
                // 添加在header上
                [_headerView addSubview:scroll];
                
#pragma mark - button 点击跳转国家实用信息的webView数据
                self.overview_urlStr = result[@"data"][@"overview_url"];
#pragma mark - 热门城市的数据处理
                NSArray *hotArr = result[@"data"][@"hot_city"];
                self.dataArr = [YZQCollectionModel modelHanderWithArray:hotArr];
#pragma mark - 获取超值自由行
                NSArray *mArr = result[@"data"][@"new_discount"];
                self.moneyArr = [YZQMoneyModel modelHanderWithArray:mArr];
                [_tableView reloadData];
                [_collectionView reloadData];
#pragma mark - 轮播图上的label 中文 国家名
                UILabel *labelCnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 120, 50)];
                [scroll addSubview:labelCnname];
                labelCnname.text = result[@"data"][@"cnname"];
                labelCnname.font = [UIFont systemFontOfSize:18];
                labelCnname.textColor = [UIColor whiteColor];
                
#pragma mark -  轮播图上的label 英文 国家名
                UILabel *labelEnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 120, 30)];
                [scroll addSubview:labelEnname];
                labelEnname.text = result[@"data"][@"enname"];
                labelEnname.textColor = [UIColor whiteColor];
                labelEnname.font = [UIFont systemFontOfSize:15];
                
                
#pragma mark -  轮播图上的label 国家简介
                UILabel *labelEntryCont = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 60)];
                labelEntryCont.text = result[@"data"][@"entryCont"];
                [scroll addSubview:labelEntryCont];
                labelEntryCont.font = [UIFont systemFontOfSize:14];
                labelEntryCont.numberOfLines = 2;
                labelEntryCont.textColor = [UIColor whiteColor];
                
                
#pragma mark -  国家使用信息按钮
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.frame = CGRectMake(WIDTH  / 2 + 63, 170, 100, 30);
                [btn setTitle:@"国家实用信息 ☞" forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont systemFontOfSize:13];
                [btn setTitleColor:[UIColor colorWithRed:0.4 green:0.7 blue:0.4 alpha:1] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
                [scroll addSubview:btn];
                
#pragma mark - collectionview创建
                UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
                // 滚动方向
                layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
                // cell大小
                layout.itemSize = CGSizeMake((WIDTH - 30) / 2, 170 / 2);
                // 行列间距
                layout.minimumInteritemSpacing = 10;
                layout.minimumLineSpacing = 10;
                // 边界
                layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
                self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 200) collectionViewLayout:layout];
                _collectionView.delegate = self;
                _collectionView.dataSource = self;
                _collectionView.backgroundColor = [UIColor whiteColor];
                
                [self.headerView addSubview:_collectionView];
                // 重用池
                [_collectionView registerClass:[YZQHotCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
                
                
                
                ;
#pragma mark - collectionView 上的view
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 210, self.view.frame.size.width, 40)];
                //        view.backgroundColor = [UIColor whiteColor];
                
                [_headerView addSubview:view];
                
                UIView *hengxianView = [[UIView alloc] initWithFrame:CGRectMake(10, 245, self.view.frame.size.width - 20, 1)];
                hengxianView.backgroundColor = [UIColor colorWithRed:137/255.0 green:137/255.0 blue:137/255.0 alpha:1];
                [_headerView addSubview:hengxianView];
                
                UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 215, 100, 30)];
                [_headerView addSubview:cityLabel];
                cityLabel.text = @"热门城市";
                cityLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
                
                
                
#pragma mark - collectioncView上的button
                UIButton *btnCollection = [UIButton buttonWithType:UIButtonTypeCustom];
                btnCollection.frame = CGRectMake(WIDTH - 90, 215, 80, 30);
                
                [_headerView addSubview:btnCollection];
                [btnCollection setTitle:@"查看全部 ☞" forState:UIControlStateNormal];
                btnCollection.titleLabel.font = [UIFont systemFontOfSize:15];
                [btnCollection setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [btnCollection addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - tableView上的View
                UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 460, self.view.frame.size.width, 30)];
                //        ziyouxingView.backgroundColor = [UIColor whiteColor];
                
                [_headerView addSubview:ziyouxingView];
                
                
                UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 460, 150, 30)];
                title.text = @"超值自由行";
                title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
                [_headerView addSubview:title];
                
            }
            
        } fail:^(NSError *error) {
            
        }];
        }else{
#pragma mark - 获取轮播图数据
        NSString *str = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/city_detail?city_id=%@",self.cid];
        NSString *url = @"&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
        [QPNetWorkingTool getNetWorkingTool:[NSString stringWithFormat:@"%@%@",str,url] dic:nil success:^(id result) {
#pragma mark - tableViewheader
            self.headerView = [[UIView alloc] initWithFrame:CGRectMake(10, 250, self.view.frame.size.width, 240)];
            _headerView.backgroundColor = [UIColor colorWithRed: 215 / 255.0 green:215 / 255.0 blue: 215 / 255.0 alpha:1];
            self.tableView.tableHeaderView = _headerView;
            
#pragma mark - 轮播图数据处理
            
            self.scrollArr = result[@"data"][@"photos"];
            
            // 轮播图
            MyLoopsView *scroll = [MyLoopsView myLoopsViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) imageArr:_scrollArr];
            // 添加在header上
            [_headerView addSubview:scroll];
#pragma mark -  button 点击国家实用信息跳转的webView数据
            self.overview_urlStr = result[@"data"][@"overview_url"];
            
#pragma mark -  获取超值自由行
            NSArray *mArr = result[@"data"][@"new_discount"];
            self.moneyArr = [YZQMoneyModel modelHanderWithArray:mArr];
            [_tableView reloadData];
            [_collectionView reloadData];
            
#pragma mark -  轮播图上的label 中文 国家名
            UILabel *labelCnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 80, 120, 50)];
            [scroll addSubview:labelCnname];
            
            labelCnname.text = result[@"data"][@"cnname"];
            labelCnname.font = [UIFont systemFontOfSize:18];
            labelCnname.textColor = [UIColor whiteColor];
            
            
#pragma mark -  轮播图上的label 英文 国家名
            UILabel *labelEnname = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 120, 30)];
            [scroll addSubview:labelEnname];
            labelEnname.text = result[@"data"][@"enname"];
            labelEnname.textColor = [UIColor whiteColor];
            labelEnname.font = [UIFont systemFontOfSize:15];
            
            
#pragma mark -  轮播图上的label 国家简介
            UILabel *labelEntryCont = [[UILabel alloc] initWithFrame:CGRectMake(30, 130, self.view.frame.size.width - 60, 60)];
            labelEntryCont.text = result[@"data"][@"entryCont"];
            [scroll addSubview:labelEntryCont];
            labelEntryCont.font = [UIFont systemFontOfSize:14];
            labelEntryCont.numberOfLines = 2;
            labelEntryCont.textColor = [UIColor whiteColor];
            
            
#pragma mark -  国家使用信息按钮
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(WIDTH / 2 + 63, 170, 100, 30);
            [btn setTitle:@"国家实用信息 ☞" forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn setTitleColor:[UIColor colorWithRed:0.4 green:0.7 blue:0.4 alpha:1] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(button) forControlEvents:UIControlEventTouchUpInside];
            [scroll addSubview:btn];
            
#pragma mark - tableView上的View
            UIView *ziyouxingView = [[UIView alloc] initWithFrame:CGRectMake(0, 210, self.view.frame.size.width, 30)];
            //            ziyouxingView.backgroundColor = [UIColor whiteColor];
            
            [_headerView addSubview:ziyouxingView];
            
#pragma mark -  超值自由行
            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 150, 30)];
            title.text = @"超值自由行";
            title.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
            
            [_headerView addSubview:title];
        } fail:^(NSError *error) {
            
        }];
    }
    
}
#pragma mark - 点击查看全部 button点击方法
- (void)jump
{
    
    
    YZQButtonCollectonController *btnVC = [[YZQButtonCollectonController alloc] init];
    NSString *url = [NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@",self.cid];
    NSString *str = @"&oauth_token=89dd34fbffaaa72503b4baa7a9d02812&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_os=ios%25207.1.2&track_user_id=7328550&v=1";
    //拼接网址
    NSString *xrsb = [NSString stringWithFormat:@"%@%@",url,str];
    btnVC.str = xrsb;
    [self.navigationController pushViewController:btnVC animated:YES];
    
    

    
}
#pragma mark - 国家实用信息button点击方法
- (void)button
{
    
OMGWebViewViewController *webVC = [[OMGWebViewViewController alloc] init];
    webVC.urlStr = _overview_urlStr;
    [self.navigationController pushViewController:webVC animated:YES];

    
    
}
#pragma mark - tableView 方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _moneyArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YZQMoneyModel *m = _moneyArr[indexPath.row];
    YZQMoneyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    cell.priceLabel.text = [[m.price stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
    cell.titleLabel.text = m.title;

    cell.titleLabel.font = [UIFont systemFontOfSize:15];
    cell.titleLabel.numberOfLines = 0;
    cell.priceoffLabel.text = m.priceoff;
    cell.priceoffLabel.font = [UIFont systemFontOfSize:15];
    cell.priceoffLabel.textColor = [UIColor colorWithRed:137 /255.0 green:137/255.0 blue:137/255.0 alpha:1];
    return cell;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
    
}
#pragma mark - cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
OMGWebViewViewController *firstVC = [[OMGWebViewViewController alloc] init];
    YZQMoneyModel *m = _moneyArr[indexPath.row];
    firstVC.urlStr = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.8.3&track_deviceid=CDA3EEEB-61BF-473E-AFFF-32B6332C9BBF&track_user_id=7328550", m.cid];
    [self.navigationController pushViewController:firstVC animated:YES];

    
    
}
#pragma mark - collectionView 方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
    
}
#pragma mark - collectioncell方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YZQCollectionModel *m = _dataArr[indexPath.row];
    YZQHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.cnnameLabel.text = m.cnname;
    cell.ennameLabel.text = m.enname;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
    
    
    
    
    
    return cell;
    
}
#pragma mark - collectionCell跳转
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YZQCityViewController *cityVC = [[YZQCityViewController alloc] init];
    YZQCollectionModel *m = _dataArr[indexPath.row];
    cityVC.str = m.strid;
    [self.navigationController pushViewController:cityVC animated:YES];
    

    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
